//
//  ScoreDetaillViewController.m
//  BaseProject
//
//  Created by hundredsandc on 2017/10/30.
//  Copyright © 2017年 hundredsandc. All rights reserved.
//

#import "ScoreDetaillViewController.h"
#import "Masonry.h"
#import "UIManage.h"
#import "SLDBCell.h"
#import "RecentScoreTableViewCell.h"
#import "LossPerCentCell.h"
#import "HTTPManagers.h"
#import "ScoreDetailModel.h"
#import "MJExtension.h"
#import "scoreAnalysisModel.h"

@interface ScoreDetaillViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,strong)UITableView *tableView;
@property (nonatomic,strong)UIView *tableHeaderView;
@property (nonatomic,strong)UIButton *popBtn;
@property (nonatomic,strong)UIImageView *headerBG;
@property (nonatomic,strong)UILabel *HeaderTitle;
@property (nonatomic,strong)UILabel *timeLabel;
@property (nonatomic,strong)UILabel *teamOneNameLabel;
@property (nonatomic,strong)UILabel *teamOneScoreLabel;
@property (nonatomic,strong)UILabel *teamTwoNameLabel;
@property (nonatomic,strong)UILabel *teamTwoScoreLabel;
@property (nonatomic,strong)UILabel *symbolLabel;
@property (nonatomic,strong)UILabel *subScoreLabel;
@property (nonatomic,strong)UIButton *headerLeftBtn;
@property (nonatomic,strong)UIButton *headerRightBtn;
@property (nonatomic,strong)UIButton *lastSelectedBtn;
@property (nonatomic,strong)NSMutableArray *historyArr;
@property (nonatomic,strong)NSMutableArray *oddsArr;
@property (nonatomic,strong)UIImageView *popImageView;
@property(nonatomic,strong)scoreAnalysisModel *analysisModel;
@end

@implementation ScoreDetaillViewController
{
    BOOL isLeft;
    NSInteger type;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"比分详情";
    isLeft = YES;
    type = 0;
    [self getData];
    [self getComparison];
    [self.view addSubview:self.tableView];
    [self setTableHeaderView];
}
-(void)getData{
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"match_id"]=self.match_id;
    
    
    
    [HTTPManagers getWithScoreURL:@"get_team_matchs" params:params success:^(id json) {
        NSDictionary *get_team_matchs = [json objectForKey:@"get_team_matchs"];
        NSArray *history_matchs = [get_team_matchs objectForKey:@"history_matchs"];
        if (self.historyArr.count>0) {
            [self.historyArr removeAllObjects];
        }
        if (history_matchs.count>0) {
            for (int i = 0; i<history_matchs.count; i++) {
                NSDictionary *dict = history_matchs[i];
                ScoreDetailModel *model = [ScoreDetailModel mj_objectWithKeyValues:dict];
                [self.historyArr addObject:model];
            }
            [_tableView reloadData];
        }
    } failure:^(NSError *error) {
        
    }];

}
-(void)getComparison{
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"match_id"]=self.match_id;
    
    
    
    [HTTPManagers getWithScoreURL:@"qry_match_analysis" params:params success:^(id json) {
        NSDictionary *dict = [json objectForKey:@"qry_match_analysis"];
        _analysisModel = [scoreAnalysisModel mj_objectWithKeyValues:dict];
        [_tableView reloadData];
    } failure:^(NSError *error) {
        
    }];
}
- (void)viewWillAppear:(BOOL)animated {
    [self.navigationController setNavigationBarHidden:YES animated:animated];
    [super viewWillAppear:animated];
}

- (void)viewWillDisappear:(BOOL)animated {
    [self.navigationController setNavigationBarHidden:NO animated:animated];
    [super viewWillDisappear:animated];
}

-(void)setTableHeaderView{
   
    _tableHeaderView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 200)];
    _tableView.tableHeaderView = _tableHeaderView;
    
    _headerBG = [[UIImageView alloc]initWithFrame:CGRectZero];
    _headerBG.image = [UIImage imageNamed:@"bg2"];
    [_tableHeaderView addSubview:_headerBG];
    
    _popBtn = [UIManage createButtonFrame:CGRectZero withBackColor:nil BackgroundImage:nil lightImageView:nil withTarget:self withAction:@selector(pop) withTitle:nil withtextfont:nil withTextColor:nil];
    [_tableHeaderView addSubview:_popBtn];
    _popImageView = [[UIImageView alloc]init];
    _popImageView.image = [UIImage imageNamed:@"fanhui_w"];
    [_popBtn addSubview:_popImageView];
    
    _HeaderTitle = [UIManage createUILabelFrame:CGRectZero withBackColor:nil withTextColor:[UIColor whiteColor] withTextFont:FONT(16) withTextAlignment:NSTextAlignmentCenter withTextContent:self.scoreModel.league_name];
    [_tableHeaderView addSubview:_HeaderTitle];
    
    _timeLabel =[UIManage createUILabelFrame:CGRectZero withBackColor:nil withTextColor:[UIColor whiteColor] withTextFont:FONT(12) withTextAlignment:NSTextAlignmentCenter withTextContent:self.scoreModel.group_date];
    [_tableHeaderView addSubview:_timeLabel];
    
    _symbolLabel = [UIManage createUILabelFrame:CGRectZero withBackColor:nil withTextColor:RGBCOLOR(0xff, 0xec, 0x00) withTextFont:FONT(17) withTextAlignment:NSTextAlignmentCenter withTextContent:@":"];
    [_tableHeaderView addSubview:_symbolLabel];

    _teamOneNameLabel = [UIManage createUILabelFrame:CGRectZero withBackColor:nil withTextColor:[UIColor whiteColor] withTextFont:FONT(17) withTextAlignment:NSTextAlignmentRight withTextContent:self.scoreModel.home_team_name];
    _teamOneNameLabel.adjustsFontSizeToFitWidth = YES;
    [_tableHeaderView addSubview:_teamOneNameLabel];

    _teamOneScoreLabel = [UIManage createUILabelFrame:CGRectZero withBackColor:nil withTextColor:RGBCOLOR(0xff, 0xec, 0x00) withTextFont:FONT(17) withTextAlignment:NSTextAlignmentRight withTextContent:self.scoreModel.home_team_score];
    [_tableHeaderView addSubview:_teamOneScoreLabel];

    _teamTwoNameLabel = [UIManage createUILabelFrame:CGRectZero withBackColor:nil withTextColor:[UIColor whiteColor] withTextFont:FONT(17) withTextAlignment:NSTextAlignmentLeft withTextContent:self.scoreModel.guest_team_name];
    _teamTwoNameLabel.adjustsFontSizeToFitWidth = YES;
    [_tableHeaderView addSubview:_teamTwoNameLabel];
    
    _teamTwoScoreLabel = [UIManage createUILabelFrame:CGRectZero withBackColor:nil withTextColor:RGBCOLOR(0xff, 0xec, 0x00) withTextFont:FONT(17) withTextAlignment:NSTextAlignmentLeft withTextContent:self.scoreModel.guest_team_score];
    [_tableHeaderView addSubview:_teamTwoScoreLabel];
    
  
    
    _subScoreLabel = [UIManage createUILabelFrame:CGRectZero withBackColor:nil withTextColor:[UIColor whiteColor] withTextFont:FONT(11) withTextAlignment:NSTextAlignmentCenter withTextContent:[NSString stringWithFormat:@"(%@:%@)",self.scoreModel.home_team_rank,self.scoreModel.guest_team_rank]];
    [_tableHeaderView addSubview:_subScoreLabel];

//    _headerLeftBtn = [UIManage createButtonFrame:CGRectZero withBackColor:RGBCOLOR(0xf2, 0xf2, 0xf2) BackgroundImage:nil lightImageView:nil withTarget:self withAction:@selector(headerBtnClick:) withTitle:@"分析" withtextfont:FONT(14) withTextColor:[UIColor blackColor]];
//    _headerRightBtn.selected = YES;
//    _lastSelectedBtn = _headerLeftBtn;
//    _headerLeftBtn.tag = 10;
//    [_tableHeaderView addSubview:_headerLeftBtn];
//    
//    _headerRightBtn = [UIManage createButtonFrame:CGRectZero withBackColor:RGBCOLOR(0xc8, 0xc8, 0xc8) BackgroundImage:nil lightImageView:nil withTarget:self withAction:@selector(headerBtnClick:) withTitle:@"赔率" withtextfont:FONT(14) withTextColor:[UIColor blackColor]];
//    _headerRightBtn.tag = 11;
    [_tableHeaderView addSubview:_headerRightBtn];
    
    
    [self addConstraint];

    
    
}
-(void)changeSegment:(UISegmentedControl *)seg{
    type = seg.selectedSegmentIndex;
    [self getLossPer];
    
}
-(void)headerBtnClick:(UIButton *)sender{
    
    if ( sender.selected == YES) {
//        _lastSelectedBtn.selected = NO;
//        [_lastSelectedBtn setBackgroundColor:RGBCOLOR(0xf2, 0xf2, 0xf2)];
//        _lastSelectedBtn = sender;
//        sender.selected = NO;
//        [sender setBackgroundColor:RGBCOLOR(0xc8, 0xc8, 0xc8)];
        
    }else if(sender.selected ==NO){
        _lastSelectedBtn.selected = NO;
        [_lastSelectedBtn setBackgroundColor:RGBCOLOR(0xc8, 0xc8, 0xc8)];
        _lastSelectedBtn = sender;
        
        
        sender.selected = YES;
        [sender setBackgroundColor:RGBCOLOR(0xf2, 0xf2, 0xf2)];
    }
    if (sender.tag==10) {
        //分析
        isLeft = YES;
        [_tableView reloadData];
    }else{
        //赔率
        isLeft = NO;
        [self getLossPer];
    }
    
}
-(void)getLossPer{
//    http://www.buyinball.com/app-web/api/odds/qry_odds?match_id=20170528000021000118281&odds_type=0
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"match_id"]=self.match_id;
    params[@"odds_type"] =@(type);
    
    
    
    [HTTPManagers getWithOddsURL:@"qry_odds" params:params success:^(id json) {
        if ([[json objectForKey:@"result_msg"]isEqualToString:@"成功"]) {
            NSDictionary *qry_odds = [json objectForKey:@"qry_odds"];
            self.oddsArr = qry_odds[@"data"];
            
        }

    [_tableView reloadData];
        
    } failure:^(NSError *error) {
        
    }];
}

-(void)addConstraint{
    [_headerBG mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_tableHeaderView.mas_top);
        make.left.equalTo(_tableHeaderView.mas_left);
        make.right.equalTo(_tableHeaderView.mas_right);
        make.height.mas_equalTo(200);
    }];
    
    [_popBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_tableHeaderView.mas_top).with.offset(20);
        make.left.equalTo(_tableHeaderView.mas_left).with.offset(0);
        make.width.mas_equalTo(50);
        make.height.mas_equalTo(50);
    }];
    [_popImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(_popBtn);
        make.width.mas_equalTo(12);
        make.height.mas_equalTo(21);
    }];
    
    [_HeaderTitle mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_tableHeaderView.mas_top).with.offset(35);
        make.centerX.equalTo(_tableHeaderView.mas_centerX);
        make.width.mas_equalTo(200);
        make.height.mas_equalTo(16);
    }];
    
    [_timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_HeaderTitle.mas_bottom).with.offset(10);
        make.centerX.equalTo(_tableHeaderView.mas_centerX);
        make.width.mas_equalTo(200);
        make.height.mas_equalTo(12);
    }];
    
    [_symbolLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_timeLabel.mas_bottom).with.offset(25);
        make.centerX.equalTo(_tableHeaderView.mas_centerX);
        make.width.mas_equalTo(10);
        make.height.mas_equalTo(17);
    }];
    
    [_teamOneScoreLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_timeLabel.mas_bottom).with.offset(25);
        make.right.equalTo(_symbolLabel.mas_left).with.offset(-10);
        make.width.mas_equalTo(40);
        make.height.mas_equalTo(17);
    }];
    
    [_teamTwoScoreLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_timeLabel.mas_bottom).with.offset(25);
        make.left.equalTo(_symbolLabel.mas_right).with.offset(10);
        make.width.mas_equalTo(40);
        make.height.mas_equalTo(17);
    }];
//
    [_teamOneNameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_timeLabel.mas_bottom).with.offset(25);
        make.right.equalTo(_teamOneScoreLabel.mas_left).with.offset(-15);
        make.width.mas_equalTo(120);
        make.height.mas_equalTo(17);
    }];
    
    [_teamTwoNameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_timeLabel.mas_bottom).with.offset(25);
        make.left.equalTo(_teamTwoScoreLabel.mas_right).with.offset(15);
        make.width.mas_equalTo(120);
        make.height.mas_equalTo(17);
    }];
    
    [_subScoreLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_symbolLabel.mas_bottom).with.offset(15);
        make.centerX.equalTo(_tableHeaderView.mas_centerX);
        make.width.mas_equalTo(100);
        make.height.mas_equalTo(11);
    }];
    
//    [_headerLeftBtn mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.bottom.equalTo(_tableHeaderView.mas_bottom);
//        make.left.equalTo(_tableHeaderView.mas_left);
//        make.width.mas_equalTo(SCREEN_WIDTH/2);
//        make.height.mas_equalTo(30);
//    }];
//    [_headerRightBtn mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.bottom.equalTo(_tableHeaderView.mas_bottom);
//        make.right.equalTo(_tableHeaderView.mas_right);
//        make.width.mas_equalTo(SCREEN_WIDTH/2);
//        make.height.mas_equalTo(30);
//    }];
    
}
-(void)pop{
    [self.navigationController popViewControllerAnimated:YES];
}
#pragma mark - tableView
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (isLeft) {
        if (section==0) {
            NSString *One1 =[NSString stringWithFormat:@"%@%@",_analysisModel.home_zl_desc,_analysisModel.home_zl_point];
            if ([One1 containsString:@"null"]||[One1 containsString:@"NULL"]) {
                return 0;
            }

            return 1; //实力对比
        }
        return self.historyArr.count; //近期战绩
    }
    return self.oddsArr.count;//赔率
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    if (isLeft) {
        return 2;
    }
    return 1;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (isLeft) {
        if (section==0) {
            return 35;
        }else{
            return 30+35;
        }
    }
    return 55;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (isLeft) {
        if (indexPath.section==0) {
            return 145;//实力分布图
        }
        return 30;//赛况
    }
    return 47;//right
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    if (isLeft) {
        if (section==0) {
            //实力对比
            UIView *headerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 35)];
            UILabel *title = [UIManage createUILabelFrame:CGRectMake(15, 15, 100, 14) withBackColor:nil withTextColor:[UIColor blackColor] withTextFont:FONT(14) withTextAlignment:NSTextAlignmentLeft withTextContent:@"实力对比"];
            [headerView addSubview:title];
            return headerView;
        }else{
        //近期战绩
            
            //实力对比
            UIView *headerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 35)];
            UILabel *title = [UIManage createUILabelFrame:CGRectMake(15, 15, 100, 14) withBackColor:nil withTextColor:[UIColor blackColor] withTextFont:FONT(14) withTextAlignment:NSTextAlignmentLeft withTextContent:@"近期战绩"];
            [headerView addSubview:title];
            
            UIView *subTitle = [[UIView alloc]initWithFrame:CGRectMake(0, 35, SCREEN_WIDTH, 30)];
            subTitle.backgroundColor = RGBCOLOR(0xf2, 0xf2, 0xf2);
            
            UILabel *label1 = [UIManage createUILabelFrame:CGRectMake(15, (30-12)/2, 30, 12) withBackColor:nil withTextColor:[UIColor blackColor] withTextFont:FONT(12) withTextAlignment:NSTextAlignmentCenter withTextContent:@"联赛"];
            
            UILabel *label2 = [UIManage createUILabelFrame:CGRectMake(15+30+200, (30-12)/2, 30, 12) withBackColor:nil withTextColor:[UIColor blackColor] withTextFont:FONT(12) withTextAlignment:NSTextAlignmentCenter withTextContent:@"赛事"];
            
            
            UILabel *label4 = [UIManage createUILabelFrame:CGRectMake(SCREEN_WIDTH-15-30, (30-12)/2, 30, 12) withBackColor:nil withTextColor:[UIColor blackColor] withTextFont:FONT(12) withTextAlignment:NSTextAlignmentCenter withTextContent:@"赛果"];

            
            UILabel *label3 = [UIManage createUILabelFrame:CGRectMake(label4.left-200-30, (30-12)/2, 30, 12) withBackColor:nil withTextColor:[UIColor blackColor] withTextFont:FONT(12) withTextAlignment:NSTextAlignmentCenter withTextContent:@"时间"];
            
            [subTitle addSubview:label1];
            [subTitle addSubview:label2];
            [subTitle addSubview:label3];
            [subTitle addSubview:label4];
            [headerView addSubview:subTitle];
            return headerView;
        }
        
    }else{
        //right
        UIView *contentView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 55)];
        
             UISegmentedControl *segment = [[UISegmentedControl alloc]initWithItems:@[@"欧赔",@"亚赔",@"大小盘"]];
            UIFont *font = [UIFont boldSystemFontOfSize:12];
            [segment setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor whiteColor],NSFontAttributeName:font} forState:UIControlStateSelected];
            [segment setTitleTextAttributes:@{NSFontAttributeName:font} forState:UIControlStateNormal];
            segment.frame = CGRectMake((SCREEN_WIDTH-231)/2, (55-23)/2, 231, 23);
        
            segment.selectedSegmentIndex = type;
            segment.tintColor = RGBCOLOR(0x25, 0x43, 0x7f);
            [segment addTarget:self action:@selector(changeSegment:) forControlEvents:UIControlEventValueChanged];
        
        [contentView addSubview:segment];
        return contentView;
    }
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (isLeft) {
        if (indexPath.section==0) {
        //实力对比
            SLDBCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([SLDBCell class]) forIndexPath:indexPath];
            [cell customOne1:[NSString stringWithFormat:@"%@%@",_analysisModel.home_zl_desc,_analysisModel.home_zl_point] One2:[NSString stringWithFormat:@"%@%@",_analysisModel.guest_zl_desc,_analysisModel.guest_zl_point] two1:[NSString stringWithFormat:@"%@%@",_analysisModel.home_zk_desc,_analysisModel.home_zk_point] two2:[NSString stringWithFormat:@"%@%@",_analysisModel.guest_zk_desc,_analysisModel.guest_zk_point] three1:_analysisModel.home_attack_desc three2:_analysisModel.guest_attack_desc four1:_analysisModel.home_defend_desc four2:_analysisModel.guest_defend_desc one1Per:_analysisModel.home_zl_per two1Per:_analysisModel.home_zk_per three1Per:_analysisModel.home_attack_per four1Per:_analysisModel.home_defend_per];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            return cell;
        }else{
        //近期战绩
            RecentScoreTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([RecentScoreTableViewCell class]) forIndexPath:indexPath];
            if (self.historyArr.count>0) {
                
            ScoreDetailModel *model = self.historyArr[indexPath.row];
            NSString *Str = [NSString stringWithFormat:@"(主)%@%@:(客)%@%@",model.home_team_name,model.home_team_score,model.guest_team_name,model.guest_team_score];
                [cell customTitle:model.league_name time:model.match_time detail:Str result:model.match_result];
            }
            return cell;
        }
    }
    LossPerCentCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([LossPerCentCell class]) forIndexPath:indexPath];
    if (self.oddsArr.count>0) {
        NSDictionary *odds = self.oddsArr[indexPath.row];
        [cell customTitle:odds[@"company_name"] one1:odds[@"first_even_odds"] one2:odds[@"first_lost_odds"] one3:odds[@"first_win_odds"] two1:odds[@"even_odds"] two2:odds[@"lost_odds"] two3:odds[@"win_odds"]];
    }
    return cell;
    
}
-(UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        [_tableView registerNib:[UINib nibWithNibName:NSStringFromClass([SLDBCell class]) bundle:nil] forCellReuseIdentifier:NSStringFromClass([SLDBCell class])];
        [_tableView registerNib:[UINib nibWithNibName:NSStringFromClass([RecentScoreTableViewCell class]) bundle:nil] forCellReuseIdentifier:NSStringFromClass([RecentScoreTableViewCell class])];
        [_tableView registerNib:[UINib nibWithNibName:NSStringFromClass([LossPerCentCell class]) bundle:nil] forCellReuseIdentifier:NSStringFromClass([LossPerCentCell class])];
        _tableView.tableFooterView = [UIView new];
    }
    return _tableView;
}
-(NSMutableArray *)historyArr{
    if (!_historyArr) {
        _historyArr = [NSMutableArray array];
    }
    return _historyArr;
}
-(NSMutableArray *)oddsArr{
    if (!_oddsArr) {
        _oddsArr = [NSMutableArray array];
    }
    return _oddsArr;
}
@end
